﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using WFNetLib;
using WFNetLib.Forms;
using System.Runtime.InteropServices;

namespace 教室客户端
{
    public partial class Form1 : Form
    {
        // 引用系统钩子
        [DllImport("user32.dll")]
        public static extern IntPtr GetActiveWindow();

        [DllImport("user32.dll")]
        public static extern IntPtr SetActiveWindow(IntPtr hwnd);
        public Form1()
        {
            InitializeComponent();
//             WFNetLib.ADO.SQLServerOP.ConnectionString = "Data Source=.\\SQL2008;Initial Catalog=WirelessGrade;User ID=sa;Password=1001001";
//             CheckResubmit("asd",5,1330,1600);
        }
//        public static bool CheckResubmit(string ex, int zuohao, int t1, int t2)
//         {
//             StringBuilder sql = new StringBuilder();
//             sql.Append("select * from TeachGrade where (ShiYan = '");
//             sql.Append(ex);
//             sql.Append("') AND (ZuoHao = ");
//             sql.Append(zuohao.ToString());
//             sql.Append(") AND (Datetime BETWEEN '");
//             sql.Append(DateTime.Now.ToString("yyyy-MM-dd"));
//             sql.Append(" ");
//             sql.Append((t1 / 100).ToString("D2"));
//             sql.Append(":");
//             sql.Append((t1 % 100).ToString("D2"));
//             sql.Append(":00' AND '");
//             sql.Append(DateTime.Now.ToString("yyyy-MM-dd"));
//             sql.Append(" ");
//             sql.Append((t2 / 100).ToString("D2"));
//             sql.Append(":");
//             sql.Append((t2 % 100).ToString("D2"));
//             sql.Append(":00')");
//             DataTable dt = WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
//             if (dt.Rows.Count == 0)
//                 return false;
//             else
//                 return true;
//         }
        public JSON_LoginData LoginData;
        Form5 f5=null;// new Form5(); 
        private void button1_Click(object sender, EventArgs e)
        {
//             if (f5 == null) //如果子窗体为空则创造实例 并显示
//             {
//                 f5 = new Form5();
//                 //f.StartPosition = FormStartPosition.CenterScreen;//子窗体居中显示
//                 IntPtr activeForm = GetActiveWindow(); // 先得到当前的活动窗体 
//                 f5.Show(); // 弹出新窗体
//                 SetActiveWindow(activeForm); // 在把焦点还给之前的活动窗体
//                 f5.AddNum(4);
//             }
//             else
//             {
//                 if (f5.IsDisposed)//若子窗体关闭 则打开新子窗体 并显示
//                 {
//                     f5 = new Form5();
//                     //f.StartPosition = FormStartPosition.CenterScreen;//子窗体居中显示
//                     IntPtr activeForm = GetActiveWindow(); // 先得到当前的活动窗体 
//                     f5.Show(); // 弹出新窗体
//                     SetActiveWindow(activeForm); // 在把焦点还给之前的活动窗体
//                     f5.AddNum(4);
//                 }
//                 else
//                 {
//                     f5.AddNum(5);
//                 }
//             }            
//             return;
            try
            {
                WFHttpWebResponse WebResponse = new WFHttpWebResponse();
                string url = "http://wxpj.hrbeu.edu.cn/webAPI/API_Login.aspx?name=";
                url += textBox1.Text;
                url += "&password=";
                url += textBox2.Text;
                HttpWebResponse hr = WebResponse.CreateGetHttpResponse(url);
                string ret = WebResponse.Content;
                //MessageBox.Show(ret);
                LoginData = JsonSerializeHelper.DeSerialize<JSON_LoginData>(ret);
                if (LoginData.success)//正常登陆
                {
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(LoginData.message);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("未知错误"+ex.Message);
            }            
        }
    }
}
